package md.lanjing.com.module_mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ClipboardUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;

import md.lanjing.com.module_mine.R;
import md.lanjing.com.module_mine.R2;
import md.lanjing.com.module_mine.contact.InviteCodeContact;
import md.lanjing.com.module_mine.bean.MyDownPersonResopnce;
import retrofit2.Response;

/**
 * 我的邀请码
 */
public class InviteCodeActivity extends MvpActivity<InviteCodeContact.InviteCodePresent> implements InviteCodeContact.IInviteCodeView {
    @BindView(R2.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R2.id.tag_flow_layout)
    TagFlowLayout tagFlowLayout;
    String inviteCode;

    List<TagBean> mList;
    String [] arrM={"M1","M2","M3","M4","M5"};
    @Override
    public void initData(Bundle savedInstanceState) {
        inviteCode = SPUtils.getString(Constant.INVITE_CODE, null, context);
        tvInviteCode.setText(inviteCode);

        mList=new ArrayList<>();
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Intent intent = new Intent(context, InviteFriendActivity.class);
                intent.putExtra(InviteFriendActivity.LEVEL,position+1);
                startActivity(intent);
                return true;
            }
        });
        mPresent.queryDownLineFriend(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_invite_code;
    }


    @OnClick(R2.id.tv_copy)
    public void onViewClicked() {
        ClipboardUtils.copyText(context,inviteCode);
        ToastUtils.showLongToast(context,"已复制到剪贴板");
    }

    @Override
    public void getDownLineFriendData(Response<MyDownPersonResopnce> response) {
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            MyDownPersonResopnce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data))return;
            mList.add(new TagBean(arrM[0],data.getM1()));
            mList.add(new TagBean(arrM[1],data.getM2()));
            mList.add(new TagBean(arrM[2],data.getM3()));
            mList.add(new TagBean(arrM[3],data.getM4()));
            mList.add(new TagBean(arrM[4],data.getM5()));
            tagFlowLayout.setAdapter(new TagAdapter(mList) {
                @Override
                public View getView(FlowLayout parent, int position, Object o) {
                    TextView view = (TextView) LayoutInflater.from(context).inflate(R.layout.tag_flow_item, tagFlowLayout,false);
                      view.setText(mList.get(position).name+"("+mList.get(position).value+")");
                    return view;
                }
            });
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected InviteCodeContact.InviteCodePresent createPresent() {
        return new InviteCodeContact.InviteCodePresent();
    }

    class TagBean{
       public String name;
        public int value;

        public TagBean(String name, int value) {
            this.name = name;
            this.value = value;
        }
    }
}
